package com.iting.transcationdemo.dao.impl;

import com.iting.transcationdemo.bean.User;
import com.iting.transcationdemo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @author iting Nie
 * @version 1.1
 * @create 2021/11/13
 */
@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    // QueryRunner queryRunner;
	JdbcTemplate jdbcTemplate;

    @Override
    public int add(User user) throws SQLException {
        String sql = "insert into user values (?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,null,user.getUsername(),user.getPassword(),user.getAge(),user.getSex(),user.getAddress(),user.getStatus());
    }

    @Override
    public int update(int id, int age) throws SQLException {
        String sql = "update user set age = age + ? where id = ?";
        return jdbcTemplate.update(sql,age,id);
    }
    @Override
    public int delete(int id) {
		String sql = "delete from user where id = ?";
		return jdbcTemplate.update(sql,id);
    }

    @Override
    public int update(User user) {
		String sql = "update user set username = ? where id = ?";
		return jdbcTemplate.update(sql,user.getUsername(),user.getId());
    }


    @Override
    public List<User> select() {
		String sql = "select * from user";
		return jdbcTemplate.queryForList(sql,User.class);
    }
}
